<!DOCTYPE >
<html>
	<head>
		<meta charset="utf-8">

		<script src="drawCharts.js"></script>
		<script src="echarts/echarts.js"></script>
		<script src="js/Dao.js"></script>
		<script src="js/GuVo.js"></script>
		<script src="js/solarlunar.min.js"></script>
		<script src="buySell/markDate.js"></script>
		<script src="js/FileUtil.js"></script>
		<script src="guStrategy.js"></script>
		<script src="DlgsPicker.js"></script>
	</head>
	<body onload="init();">
	
		<div id="cnt">
           
			<div style='display: flex;'>
			<div>
				<input type="checkbox" v-model="buyModel" value="buyByMinPrice">
				最低买入价格<input v-model="minPrice" type="number" @change="drawChart" style="width: 50px;" />
				<br />
				<input type="checkbox" v-model="buyModel" value="buyByUpDownDay" >涨跌天数设定买入
				跌：<input v-model="downday" type="number" @change="drawChart" style="width: 50px;" />日,
					下跌比例：<input v-model="threeDayDownPersent" type="number" @change="drawChart" style="width: 50px;"/>
		    	升：<input v-model="upday" type="number" @change="drawChart"  style="width: 50px;"/>日,<br />
					<input type="checkbox"v-model="buyModel"  value="buyByTopFive">收市价突破5日均线<br />
				<input type="checkbox"v-model="buyModel"  value="buyByFiveUpTw">5日均线突破20日均线买入<br />
				<input type="checkbox" v-model="buyModel" value="buyByRedUp">低位锤子买入<br />
				<input type="checkbox" v-model="buyModel" value="buyByDLGS">到拉各斯<br />
					<input type="checkbox" v-model="buyModel" value="buyByThreeLine">三线分离<br />
					<input type="checkbox" v-model="buyModel" value="buyByKDJ">KDJ买入<br />
				
			
			</div>
			
			
			<div>
					<input type="checkbox" v-model="sellModel" value="sellByMaxPrice">
			卖出入价格<input v-model="maxPrice" type="number" @change="drawChart" style="width: 50px;" />
			<br />
				<input type="checkbox" v-model="sellModel" value="sellByDownPersent">
				高位止损：<input v-model="downPersent" type="number" @change="drawChart" />%
				中位止损盈利位置：<input v-model="minPersent" type="number" @change="drawChart"/>-<input v-model="maxPersent" type="number" @change="drawChart"/>
				中位止损：<input v-model="middlePersent" type="number" @change="drawChart"/>%
				<select v-model="topFlag" @change="drawChart">
					<option  value="open">开盘价</option>
					<option  value="close">收盘价</option>
					<option  value="max">最高价</option>
					<option  value="min">最低价</option>
				</select>
				<br />
				
				<input type="checkbox"v-model="sellModel"  value="sellByDownFive">5日均线下跌<br />
				<input type="checkbox"v-model="sellModel"  value="sellByFiveDownTW">5日均线下跌20日均线<br />
				<input type="checkbox" v-model="sellModel" value="sellByGreenDown">高位锤子卖出<br />
				<input type="checkbox" v-model="sellModel" value="sellByGreen">一跌就跑<br />
				<input type="checkbox"v-model="sellModel"  value="sellByRed">赚钱就跑<br />
				
			</div>
			
			</div>
			<br />
			<select v-model="selYear" @change="drawChart">
				<option v-for="item in year" :value="item">{{item}}</option>
			</select>
			<button onclick="saveModel();">保存策略</button>
			<div>{{result}}</div>

			<div id="chat" style="width: 100%;height: 500px;"></div>
			<div id="chat2" style="width: 100%;height: 500px;"></div>
			<div v-for="item in list" style="display: flex;height:30px;line-height:30px;">
				<div style="width:150px;">{{item.propCN}}:</div>
				<div>{{item.val}}</div>
			</div>
		</div>
	</body>
</html>
<script src="js/vue.min.js"></script>
<script src="Strategy2.js"></script>
<script src="buySell/buySell.js"></script>
<script src="buySell/buys/buyByDLGS.js"></script>
<script src="buySell/buys/buyByFiveUpTw.js"></script>
<script src="buySell/buys/buyByRedUp.js"></script>
<script src="buySell/buys/buyByUpDownDay.js"></script>
<script src="buySell/buys/buyByTopFive.js"></script>
<script src="buySell/buys/buyByThreeLine.js"></script>
<script src="buySell/buys/buyByKDJ.js"></script>
<script src="buySell/buys/buyByMinPrice.js"></script>

<script src="buySell/sells/sellByDownFive.js"></script>
<script src="buySell/sells/sellByDownPersent.js"></script>
<script src="buySell/sells/sellByFiveDownTW.js"></script>
<script src="buySell/sells/sellByGreen.js"></script>
<script src="buySell/sells/sellByGreenDown.js"></script>
<script src="buySell/sells/sellByRed.js"></script>
<script src="buySell/sells/sellByMaxPrice.js"></script>


<script src="buySell/extends/KDJ.js"></script>
<script src="buySell/extends/Utils.js"></script>
<script>
	let app;

	function init() {
		var pars = decodeURI(location.href);
		let code = pars.split("=")[1].split("&")[0];
		let rs = guStrategy[code];
		app = new Vue({
			el: "#cnt",
			mixins:[rs?rs:{}],
			data: function() {

				return {
					buyModel:rs?rs.buyModel:["buyByUpDownDay"],
					sellModel:rs?rs.sellModel:["sellByDownPersent"],
					result: '',
					minPrice:rs?rs.minPrice:0,
					maxPrice:rs?rs.maxPrice:0,
					upday: rs?rs.upday:1,
					downday: rs?rs.downday:3,
					downPersent: rs?rs.downPersent:0.97,
					middlePersent:rs?rs.middlePersent:0.9,
					maxPersent:rs?rs.maxPersent:0.15,
					minPersent:rs?rs.minPersent:0.1,
					threeDayDownPersent:rs?rs.threeDayDownPersent:0,
					topFlag:rs?rs.topFlag:'max',
					preYearMin: 0,
					selYear: "",
					allYear: {},
					allMonth: {},
					year: [],
					list: []
				}
			},
		});

		let startDate = "2025-01-01";
		let endDate = "2025-12-31";
		let preStartDate = "2024-01-01";
		let preEndDate = "2024-12-31";
		



		Strategy.cul(code, startDate, endDate, preStartDate, preEndDate, (res, allYear, allMonth) => {
			app.list = res;
			for (let key in allYear) {
				app.year.push(key);
				app.selYear = key;
			}
			app.allYear = allYear;
			app.allMonth = allMonth;
			culMonth();
			drawChart();

		});
	}

	function culMonth() {
		let month = new Date().Format("yyyy-MM-dd").substr(5, 2);
		let toalM = app.allMonth["rm" + month] / (app.allMonth["rm" + month] + app.allMonth["gm" + month]); //当月红的概率

		//debugger;
	}

	function drawLine(dates, powers) {
		let dom = document.getElementById("chat2");
		let option = {
			xAxis: {
				type: 'category',
				data: dates
			},
			yAxis: {
				type: 'value'
			},
			series: [{
				data: powers,
				type: 'bar'
			}]
		};
		let myChart;
		myChart = echarts.init(dom);
		myChart.setOption(option);

	}

	function drawChart() {
		let dom = document.getElementById("chat");
		let dom2 = document.getElementById("chat2")
		let selYear = JSON.parse(JSON.stringify(app.allYear[app.selYear]));
		if (app.selYear == "2025") {
			selYear = JSON.parse(JSON.stringify(app.allYear['2024'])).concat(selYear);
		}
		let dates = [];
		let datas = [];
		let powers = [];
		selYear.map(res => {
			dates.push(res.shift());
			datas.push(res);
			powers.push(parseFloat(res[4]) );
		});
		//drawLine(dates,powers);
		let lines = [];
		app.list.map(item => {
			(item.propCN.indexOf(app.selYear) != -1) ? lines.push(item): null; //选择今年最大最小值
			(item.propCN.indexOf(app.selYear - 1) != -1) ? lines.push(item): null; //上年最大最小值
			item.propCN == "上年最低" ? (app.preYearMin = item.val) : null;
		});

		let markLine = [];

		lines.map(item => {
			let mkLine = {
				yAxis: item.val,
				label: {
					formatter: item.propCN + ":" + item.val
				}
			}
			markLine.push(mkLine);
		});
		//买点
		
		BuySell.init(app.buyModel,app.sellModel,app.upday,app.downday,app.downPersent,app.threeDayDownPersent,app.topFlag,app.minPersent,app.maxPersent,app.middlePersent,app.minPrice,app.maxPrice);
		
		let buySell = BuySell.bs1(selYear, dates);
       
	   
		let chinaDate = MarkDate.getMarkDate(selYear, dates);
		computeRs(buySell);
		//.concat(chinaDate)
		var dg = getdlgs(datas,dates);
		//drawLineDgls(dg.dates,dg.list,dom2);
		let kdj = KDJ.getKDJ(selYear,9);
		//kdj.j;
		let idxs = Utils.findPeaks(kdj.j);
		let points = conputeNodes(idxs.lows,dates,kdj.j);
		drawKDJLines(dates,kdj.k,kdj.d,kdj.j,dom2,points);
		drawK(dom, dates, datas, buySell, markLine, powers,getPJ(5,datas),getPJ(10,datas),getPJ(20,datas));

	}
	
	
	function conputeNodes(arr,dates,js){
		let nodes = [];
		for(let i = 0;i<arr.length;i++){
			arr[i];
			var node = {};
			node.value = dates[arr[i]].substr(5,5);
			node.xAxis = dates[arr[i]];
			node.yAxis = js[arr[i]];
			node.itemStyle = {
				color: "rgba(255,0,0,0.5)"
			};
			if(node.yAxis<200){
				nodes.push(node);
			}
			
		}
		return nodes;
	}
	function getPJ(day,datas){
		let dataPJ=[];
		for(let i = 0;i<datas.length;i++){
			if(i<(day-1)){
				dataPJ.push("-");
			}else{
				let sum=0;
				for(let j=0;j<day;j++){
					sum+=parseFloat(datas[i-j][1]);
				}
				dataPJ.push(sum/day);
			}
			
		}
		return dataPJ;
	}

	function computeRs(arr) {
		let buy = 0;
		let buyTotal = 0;
		let sellTotal = 0;
		arr.forEach(item => {
			if (item.value.indexOf("B") != -1) {
				buyTotal += (-item.yAxis * 100 + 5);
			} else {
				sellTotal += (item.yAxis * 100 * 0.999 - 5);
			}
			buy += (item.value.indexOf("B") != -1 ? (-item.yAxis * 100 + 5) : (item.yAxis * 100 * 0.999 - 5));
			let str = item.value.indexOf("B") != -1 ? "buy:" + item.yAxis : "sell:" + item.yAxis;
			//console.log(str);

			// console.log(item.value)
		})
		app.result = "buy:" + buyTotal + ",sell:" + sellTotal + ",total:" + (buyTotal + sellTotal);
		console.log("buy:" + buyTotal);
		console.log("sell:" + sellTotal);
		console.log("total:" + (buyTotal + sellTotal));
	}
	function saveModel(){
		
			var pars = decodeURI(location.href);
	    let code = pars.split("=")[1].split("&")[0];
		let result = {
			buyModel:app.$data.buyModel,
			sellModel:app.$data.sellModel,
			upday: app.$data.upday,
			downday: app.$data.downday,
			downPersent: app.$data.downPersent,
			threeDayDownPersent:app.$data.threeDayDownPersent,
			middlePersent:app.$data.middlePersent,
			maxPersent:app.$data.maxPersent,
			minPersent:app.$data.minPersent,
			topFlag:app.$data.topFlag,
			minPrice:app.$data.minPrice,
			maxPrice:app.$data.maxPrice
		};
		guStrategy[code]=result;
		let data = "let guStrategy="+JSON.stringify(guStrategy);
		saveAs(data,"guStrategy.js","text/plain;charset=utf-8");
		
	}
</script>
